package com.nth.rooster.authentication.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nth.rooster.authentication.beans.LoginBean;
import com.nth.rooster.authentication.beans.RegistrationBean;
import com.nth.rooster.authentication.services.AuthenticationServiceI;
import com.nth.rooster.authentication.services.UsersServiceI;
import com.nth.rooster.core.pojo.CircleBeans;
import com.nth.rooster.core.pojo.MenuBean;
import com.nth.rooster.core.pojo.User;

@Controller
@RequestMapping("/user")
public class UserController {
	static Logger LOGGER = Logger.getLogger(UserController.class);

	@Autowired
	private UsersServiceI usersServiceI;
	
	@Autowired
	private AuthenticationServiceI authenticationServiceI;
	/**
	 *
	 * saves and updates User Profile
	 * 
	 * @param registrationBean
	 */
	@RequestMapping(value = "/saveProfile", method = RequestMethod.POST)
	@ResponseBody
	public void saveRegistration(@RequestBody RegistrationBean registrationBean){
		LOGGER.debug("Entered::Authentication::UserController::getAllInternalUsers");
		 usersServiceI.saveRegistration(registrationBean);
		LOGGER.debug("Entered::Authentication::UserController::getAllInternalUsers");
	}
	
	/**
	 * 
	 * Fetch User Profile Details
	 * 
	 * @param userID
	 * @return
	 */
	@RequestMapping(value = "/getUserProfile", method = RequestMethod.GET)
	@ResponseBody
	public RegistrationBean getUserData(@RequestParam Long userID){
		LOGGER.debug("Entered::Authentication::UserController::getUserData::userID::"+userID);
		return usersServiceI.getUserData(userID);
	}
	
	
	/**
	 * 
	 * Fetch User Profile Details
	 * 
	 * @param userID
	 * @return
	 */
	@RequestMapping(value = "/updateUserCredentials", method = RequestMethod.GET)
	@ResponseBody
	public void updateUserCredentials(@RequestParam LoginBean loginBean){
		LOGGER.debug("Entered::Authentication::UserController::updateUserCredentials::userID::"+loginBean.getUserID());
		
		authenticationServiceI.updateUserCredentials(loginBean);
		
		LOGGER.debug("Exit::Authentication::UserController::updateUserCredentials::userID::"+loginBean.getUserID());
	}
	
	/**
	 * 
	 * 
	 * @param userID
	 * @return
	 */
	@RequestMapping(value = "/getUserMenu", method = RequestMethod.GET)
	@ResponseBody
	public Set<MenuBean> getUserMenu(@RequestParam Long userID){
		LOGGER.debug("Entered::Authentication::UserController::getUserMenu::userID"+userID);
		Set<MenuBean> menus= usersServiceI.getUserMenu(userID);
		LOGGER.debug("Exit::Authentication::UserController::getUserMenu::userID"+userID);
		
		return menus;
	}
	
	@RequestMapping(value = "/getAllInternalUsers", method = RequestMethod.GET)
	@ResponseBody
	public List<User> getAllInternalUsers() {
		LOGGER.debug("Entered::Authentication::UserController::getAllInternalUsers");
		List<User> internalUsers= usersServiceI.getAllInternalUsers();
		LOGGER.debug("Entered::Authentication::UserController::getAllInternalUsers");
		return internalUsers;
	}
	
	@RequestMapping(value = "/getIT", method = RequestMethod.GET)
	@ResponseBody
	public RegistrationBean getIT(@RequestParam Long userID){
		RegistrationBean menus = new RegistrationBean();
		List<CircleBeans> beans = new ArrayList<CircleBeans>();
		CircleBeans circleBeans = new CircleBeans();
		circleBeans.setCircleID(1l);
		beans.add(circleBeans);
		menus.setCircleRequestedForAuthorization(beans);
		return menus;
	}
	
	
	public UsersServiceI getUsersServiceI() {
		return usersServiceI;
	}

	public void setUsersServiceI(UsersServiceI usersServiceI) {
		this.usersServiceI = usersServiceI;
	}
}
